//
//  SJMainBar.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/9/29.
//  Copyright © 2015年 world. All rights reserved.
//
//  自定义tabBar

import UIKit

class SJMainBar: UITabBar {

    
    //懒加载创建加号按钮
    lazy var plusBtn:UIButton = {
        let btn = UIButton()
        self.addSubview(btn)
        
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        return btn
        }()
    
    //布局自控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //UITabBarButton是私有接口,在开发中不要使用
//        print(items)
        //swift中可以直接取到宽高不需要通过size
        let btnCount = 5
        let w = bounds.width / CGFloat(btnCount)
        let h = bounds.height
        var index = 0
        let rect = CGRectMake(0, 0, w, h)
        for v in subviews{
            //按钮是tabBarButton但是不是加号按钮的时候
            if v is UIControl && !(v is UIButton){
                //CGRectOffSet在某个rect的基础上进行偏移
                v.frame = CGRectOffset(rect, CGFloat(index) * w , 0)
                index += (index==1 ? 2 : 1)
                
            }
        }
        plusBtn.frame = CGRectMake(w*2, 0, w, h)
    }
}
